package com.stars.home.adapter

import android.widget.FrameLayout
import android.widget.ImageView
import coil.load
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.util.SmartGlideImageLoader
import com.qmuiteam.qmui.kotlin.onClick
import com.stars.home.R
import com.stars.home.model.GoodsImgBean
import com.stars.home.vmodel.OpinionVModel
import java.util.*


/**
 *
 *on 2020/9/28
 */
class DynamicAdapter(var vm: OpinionVModel, data: MutableList<GoodsImgBean>) :
    BaseMultiItemQuickAdapter<GoodsImgBean, BaseViewHolder>(data) {

    init {
        addItemType(-1, R.layout.item_add)
        addItemType(0, R.layout.item_add_pic)
    }

    override fun convert(helper: BaseViewHolder, item: GoodsImgBean) {
        when (helper.itemViewType) {
            0 -> {//查看图片
                if (item.chose) {//正在上传中
                    helper.getView<ImageView>(R.id.img_attachment).load(R.drawable.bg_need_error)
                    helper.setVisible(R.id.iv_delete, false)

                } else {
                    helper.getView<ImageView>(R.id.img_attachment).load(item.url)
                    helper.setVisible(R.id.iv_delete, true)
                    var ivDelete = helper.getView<ImageView>(R.id.iv_delete)

                    var ivAttachment = helper.getView<ImageView>(R.id.img_attachment)


                    ivDelete.onClick {
                        vm.clearMoreImg(helper.adapterPosition)
                        remove(item)
                        notifyDataSetChanged()

                    }
                    ivAttachment.onClick {
                        //查看大图


                        var list = mutableListOf<Any>()
                        list.add(item.url.toString())


                        XPopup.Builder(context)
                            .asImageViewer(
                                ivAttachment, 0, list,
                                { popupView, position ->
                                    popupView.updateSrcView( ivAttachment)
                                }, SmartGlideImageLoader()
                            ).isShowSaveButton(false)
                            .show()

                    }

                }


//                helper.addOnClickListener(R.id.iv_delete)
//                helper.addOnClickListener(R.id.img_attachment)
            }
            -1 -> {//上传图片
                var fl = helper.getView<FrameLayout>(R.id.fl)
                fl.onClick {
                    vm.choseMoreImg()
                }

//                helper.addOnClickListener(R.id.fl)
            }

        }
    }


}
